package com.design.testdemo.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.design.testdemo.R;

public class TimeActivity extends AppCompatActivity {

    private Button mButton;
    private CountDownTimer mCountDownTimer;

    private boolean isTimeStart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time);
        mCountDownTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                mButton.setText("倒计时" + millisUntilFinished / 1000);
            }

            @Override
            public void onFinish() {
                stop();
            }
        };
        mButton = (Button) findViewById(R.id.button);
        Button btnStop = (Button) findViewById(R.id.stop);

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                start();
            }
        });

        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCountDownTimer.cancel();
                stop();
            }
        });
    }

    private void start() {
        if (isTimeStart) {
            return;
        }
        isTimeStart = true;
        mCountDownTimer.start();
    }

    private void stop() {
        isTimeStart = false;
        mButton.setText("结束");
    }
}
